package com.example.wacoffee_manage.controller;

import com.example.wacoffee_manage.Service.orderService;
import com.example.wacoffee_manage.Service.paymentService;
import com.example.wacoffee_manage.pojo.Order;
import com.example.wacoffee_manage.utils.Result;
import com.example.wacoffee_manage.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/payments")
public class paymentController {

    @Autowired
     private paymentService paymentService;
    @Autowired
    private orderService orderService;
    @GetMapping
    public Result get(@RequestParam int id){
        Order order=orderService.getById(id);
        if (order == null ) {
            return Result.error("无效订单");
        }
        return Result.success(order);
    }

    @PutMapping("/pay")
    public Result pay(@RequestParam int id) throws IOException {
            paymentService.pay(id);
            WebSocketServer.sendMessage();
            return Result.success();
    }

    @PutMapping("/cancelPay")
    public Result cancelPay(@RequestParam int id){
        paymentService.cancelPay(id);
        return Result.success();
    }
}